package com.example.takeaway.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.takeaway.common.BaseContext;
import com.example.takeaway.common.R;
import com.example.takeaway.entity.AddressBook;
import com.example.takeaway.service.AddressBookService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/addressBook")
@AllArgsConstructor
public class AddressBookController {

    private final AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */


    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook){


        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}",addressBook);
        addressBookService.save(addressBook);
        return R.success(addressBook);


    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */

    @PutMapping ("/default")
    public R<AddressBook> SetDefault(@RequestBody AddressBook addressBook){


        log.info("addressBook{}",addressBook);

        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault,0);

        //SQL : update address_book set is_default = 0 where user_isd = ?
        addressBookService.update(wrapper);

        addressBook.setIsDefault(1);

        //SQL:update address_book set is_default = 1 where id = ?
        addressBookService.updateById(addressBook);

        return R.success(addressBook);


    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */

    @GetMapping("/{id}")
    public R get(@PathVariable Long id){

        AddressBook addressBook = addressBookService.getById(id);

        if(addressBook != null){
            return R.success(addressBook);
        }else{
            return R.error("没有找到对象");
        }


    }


    /**
     * 查询默认地址
     */

    @GetMapping("default")
    public R<AddressBook> GetDefault() {

        //构造条件
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.eq(AddressBook::getIsDefault, 1);

        AddressBook one = addressBookService.getOne(wrapper);

        if (one != null) {
            return R.success(one);
        } else {
            return R.error("没有找到该对象");
        }

    }

    /**
     * 查询指定用户的全部地址
     * @param addressBook
     * @return
     */


    @GetMapping("/list")
    public R<List<AddressBook>> ListAddress(AddressBook addressBook){

        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}",addressBook);

        //条件构造器
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(addressBook.getUserId()!=null , AddressBook::getUserId,addressBook.getUserId());
        wrapper.orderByDesc(AddressBook::getUpdateTime);

        return R.success(addressBookService.list(wrapper));

    }


}
